# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os

from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout

required_conan_version = ">=2.13.0"


class GrpcToolConan(ConanFile):
    name = "grpc_tool"
    description = "grpc tool"
    settings = "os", "compiler", "build_type", "arch"
    exports_sources = ["permissions.ini"]
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
        "with_zlib": [True, False],
        "with_openssl": [True, False],
        "with_cares": [True, False],
        "with_abseil": [True, False],
        "build_codegen": [True, False]
    }
    default_options = {
        "shared": True,
        "fPIC": True,
        "with_zlib": False,
        "with_openssl": False,
        "with_cares": True,
        "with_abseil": True,
        "build_codegen": True
    }
    _cmake = None

    def layout(self):
        cmake_layout(self)

    def source(self):
        source = self.conan_data["sources"][self.version]
        git = Git(self)
        git.fetch_commit(url=source["url"], commit=source["branch"])

    def requirements(self):
        requires = self.conan_data.get("requires", {}).get(self.version, {})
        for req in requires:
            self.requires(req)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.extra_cflags = ["-w"]
        tc.extra_cxxflags = ["-w"]
        absl_rootpath = self.dependencies["abseil"].package_folder
        protobuf_rootpath = self.dependencies["protobuf"].package_folder
        cares_rootpath = self.dependencies["c_ares"].package_folder
        re2_rootpath = self.dependencies["re2"].package_folder
        absl_dir = os.path.join(absl_rootpath, "lib", "cmake", "absl")
        re2_dir = os.path.join(re2_rootpath, "lib", "cmake", "re2")
        tc.variables["gRPC_INSTALL"] = True
        tc.variables["gRPC_BUILD_TEST"] = False
        tc.variables["gRPC_BUILD_CSHARP_EXT"] = False
        tc.variables["gRPC_ZLIB_PROVIDER"] = "package"
        tc.variables["gRPC_SSL_PROVIDER"] = "package"
        tc.variables["gRPC_CARES_PROVIDER"] = "package"
        tc.variables["gRPC_ABSL_PROVIDER"] = "package"
        tc.variables["gRPC_RE2_PROVIDER"] = "package"
        tc.variables["gRPC_PROTOBUF_PROVIDER"] = "package"
        tc.variables["CMAKE_CXX_STANDARD"] = 17
        tc.variables["CMAKE_PREFIX_PATH"] = (
            f"{protobuf_rootpath};{re2_dir};{cares_rootpath};{absl_dir}"
        )
        tc.generate()


    def build(self):
        if self.settings.arch != "x86_64":
            return
        cmake = self._configure_cmake()
        cmake.build()

    def package(self):
        if self.settings.arch != "x86_64":
            return
        cmake = self._configure_cmake()
        cmake.install()

    def package_info(self):
        if self.settings.arch != "x86_64":
            return
        self.cpp_info.libdirs = ["lib"]
        self.cpp_info.bindirs = ["bin"]

    def _configure_cmake(self):
        if self._cmake:
            return self._cmake
        self._cmake = CMake(self)
        self._cmake.configure(cli_args=["--no-warn-unused-cli"])
        return self._cmake